﻿using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Eneta.Portal.Common;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace Eneta.Portal.Forum
{
    public class MasterBaseWithDesignHack : MasterPage
    {
        protected HtmlControl _hackContainer;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (Request.QueryString["control"] == "ViewMessages" ||
                Request.QueryString["control"] == "UpdateMessage")
                if (_hackContainer != null)
                    _hackContainer.Attributes["class"] = "thread";
        }

        public void SetRssLinks(RssLinksDto dto)
        {
            if (dto == null)
            {
                return;
            }
            //if(dto.RssVisible)
            //{
                var rssResourceString = "$Resources:EnetaSite, ForumAllRss";
                var rssTitle = SPUtility.GetLocalizedString(rssResourceString, "EnetaSite", SPContext.Current.Web.Language);

            
            var rssLink = FindControl("rssLinkUpper") as HtmlAnchor;

                if(rssLink != null)
                {
                    if (!string.IsNullOrEmpty(dto.RssUrl))
                    {
                        rssLink.HRef = dto.RssUrl;
                        //rssLink.InnerText = dto.RssTitle;
                        rssLink.InnerText = rssTitle;
                        rssLink.Visible = true;
                    }
                    else
                    {
                        rssLink.Visible = false;
                    }
                }
                
                rssLink = FindControl("rssLinkBottom") as HtmlAnchor;
                if (rssLink != null)
                {
                    if (!string.IsNullOrEmpty(dto.RssUrl))
                    {
                        rssLink.HRef = dto.RssUrl;
                        //rssLink.InnerText = dto.RssTitle;
                        rssLink.InnerText = rssTitle;
                    }
                    else
                    {
                        
                        rssLink.Visible = false;
                    }
                }
            //}

            var container = FindControl("rssLinkContainer");
            if (container != null)
                container.Visible = !string.IsNullOrEmpty(dto.RssUrl); // dto.RssVisible;
            container = FindControl("rssLinkBottomContainer");
            if (container != null)
                container.Visible = !string.IsNullOrEmpty(dto.RssUrl);  //dto.RssVisible;

            //if(dto.CommentsRssVisible)
            //{
                var commentsResourceString = "$Resources:EnetaSite, ForumAllRss";
                var commentsTitle = SPUtility.GetLocalizedString(commentsResourceString, "EnetaSite", SPContext.Current.Web.Language);

                rssLink = FindControl("rssCommentsLinkUpper") as HtmlAnchor;
                if (rssLink != null)
                {
                    rssLink.HRef = dto.CommentsRssUrl;
                    rssLink.InnerText = commentsTitle;
                }

                rssLink = FindControl("rssCommentsLinkBottom") as HtmlAnchor;
                if (rssLink != null)
                {
                    rssLink.HRef = dto.CommentsRssUrl;
                    rssLink.InnerText = commentsTitle;
                }
            //}
            container = FindControl("rssCommentsLinkContainer");
            if (container != null)
                container.Visible = !string.IsNullOrEmpty(dto.CommentsRssUrl); //dto.CommentsRssVisible;
            container = FindControl("rssCommentsLinkBottomContainer");
            if (container != null)
                container.Visible = !string.IsNullOrEmpty(dto.CommentsRssUrl);  //dto.CommentsRssVisible;

            var rssHeaderLink = FindControl("") as HtmlLink;
            if (rssHeaderLink != null)
            {
                rssHeaderLink.Href = EnetaApplication.PortalForumRssUrl;
                rssHeaderLink.Attributes["Title"] = SPContext.Current.Web.Title;
            }
        }
    }
}
